﻿using System;
using Adaptive.Agrona.Concurrent.RingBuffer;
using Unity.Commons.Client.Buffer;

namespace Unity.Commons.Client.Configuration
{
    public class DefaultConfiguration : IUnityConfiguration
    {
        protected const int DefaultMainBufferSize = 1024;
        protected const int DefaultOfflineBufferSize = 4096;

        private const int MaximumBufferSizeBytes = 1073742592;
        protected const int MaximumBufferSizeMessages = 12201611;

        public static int CalculateMaximumBufferSizeBytes()
        {
            return (HighestOneBit(int.MaxValue) + RingBufferDescriptor.TrailerLength);
        }

        public static int CalculateMaximumBufferSizeMessages()
        {
            return (MaximumBufferSizeBytes - RingBufferDescriptor.TrailerLength) / UnityRingBuffer.GetRecordLength();
        }
        protected static int HighestOneBit(int value)
        {
            return (int)Math.Pow(2, Convert.ToString(value, 2).Length - 1);
        }

        public int GetMainBufferSize()
        {
            return DefaultMainBufferSize;
        }

        public int GetOfflineBufferSize()
        {
            return DefaultOfflineBufferSize;
        }
    }
}